package com.action;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oracle.jdbc.pool.OracleDataSource;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.util.HbnUtil;

import entities.Product;
import entities.Seller;

/**
 * Servlet implementation class DeleteProduct
 */
@WebServlet("/DeleteProduct")
public class DeleteProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteProduct() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Session session = HbnUtil.getSession();
		Transaction tran = session.beginTransaction();
		HttpSession se = request.getSession(true);
		//Product p = new Product();
		//Seller seller = new Seller();
		//seller = (Seller) session.createQuery("from Seller where userid =" +se.getAttribute("Userid")).uniqueResult();
		
		/*p.setProductid(Long.parseLong(request.getParameter("Productid")));
		p.setName(request.getParameter("Name"));
		p.setDescription(request.getParameter("Description"));
		p.setPrice(Double.parseDouble(request.getParameter("Price")));
		p.setDiscount(Double.parseDouble(request.getParameter("Discount")));
		p.setImgpath(request.getParameter("Imgpath"));*/
		//p.setSeller(seller);
		//session.createSQLQuery("delete from Product where productid='" + p.getProductid()+"'");
		//Product p = (Product) session.load(Product.class, Long.parseLong(request.getParameter("Productid")));
		Product p = (Product) session.createQuery("from Product where productid='" + request.getParameter("Productid") + "'").uniqueResult();
		session.delete(p);
		tran.commit();
		response.sendRedirect("productedit.jsp");
	
		
		/*	Connection conn = null;
		ResultSet rset = null;
		String s= request.getParameter("Productid");
		System.out.print(s);
		String error_msg = "";
		try {
		OracleDataSource ods = new OracleDataSource();
		ods.setURL("jdbc:oracle:thin:scott/tiger@//w4111b.cs.columbia.edu:1521/ADB");
		conn = ods.getConnection();
		Statement stmt = conn.createStatement();
		rset = stmt.executeQuery("DELETE from Product where productid ='" + s+"'");
		} catch (SQLException e) {
		error_msg = e.getMessage();
		if( conn != null ) {
		try {
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		}
		}
		
		response.sendRedirect("productedit.jsp");*/
		
	}

}
